//electron udp socket.
window.electron = require('electron');
const ipcRenderer = window.electron.ipcRenderer; 
const contextBridge = window.electron.contextBridge; 
//渲染进程发送消息给主进程
ipcRenderer.send('receiving-broadcast');
//主进程发送消息给渲染进程
ipcRenderer.on('broadcast-reply', (event, message) => {
    // console.log('rinfo:', arg.rinfo);
    // let msg = Uint8ArrayToString(arg.msgs); 
    // let msg = arg.msgs;
    // let udpMsg = msg.substr(msg.indexOf('elec:') + 5);
    // console.log('msg:', udpMsg);
    console.log('msg:', message);
    let myEvent = new CustomEvent('udpreceivemsg', { "detail": message, bubbles: true, cancelable: false });
    // let myEvent = new CustomEvent('udpreceivemsg', { "detail": {"msg":udpMsg}, bubbles: true, cancelable: false });
    document.dispatchEvent(myEvent);
    // setTimeout(() => {
    //     ipcRenderer.send('receiving-broadcast');
    // }, 200);

})


// /**
//  * [sendMsg 发送消息]
//  * @param  {[type]} inf [消息数据]
//  * @return {[type]}     [description]
//  */
// function sendMsg(inf){
//     console.log('sendMsg inf:',inf);
//     ipcRenderer.send('send-broadcast',inf);
// }


/*
* 搭建主进程和渲染进程的桥梁
*/ 
const sendMainInfo = async (val) => {
    ipcRenderer.invoke('render-info',val);
  }

// electronAPI 代表向渲染进程传递的对象命名，sendMainInfo表示向渲染进程传递一个回调函数
contextBridge.exposeInMainWorld('electronAPI',{
    platform: process.platform,
    sendMainInfo,
  });



 